package com.mallcloud.mall.mall.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.mall.api.entity.MallMerchantModel;
import com.mallcloud.mall.mall.service.MallMerchantService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商户管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/mall/merchant")
public class MallMerchantController {
    private final MallMerchantService mallMerchantService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('mall_merchant_view')")
    public R<IPage<MallMerchantModel>> page(Page page, MallMerchantModel dto) {
        return R.ok(mallMerchantService.page(page, Wrappers.query(dto)));
    }

    @ApiOperation("列表查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('mall_merchant_view')")
    public R<List<MallMerchantModel>> list(MallMerchantModel dto) {
        return R.ok(mallMerchantService.list(Wrappers.query(dto)));
    }

    @ApiOperation("通过id查询")
    @GetMapping("/getInfo")
    @PreAuthorize("@pms.hasPermission('mall_merchant_view')")
    public R<MallMerchantModel> getInfo(MallMerchantModel dto) {
        return R.ok(mallMerchantService.getById(dto.getId()));
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('mall_merchant_add')")
    public R<Boolean> save(@RequestBody MallMerchantModel dto) {
        return R.ok(mallMerchantService.save(dto));
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('mall_merchant_edit')")
    public R<Boolean> updateById(@RequestBody MallMerchantModel dto) {
        return R.ok(mallMerchantService.updateById(dto));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('mall_merchant_del')")
    public R<Boolean> removeById(@PathVariable Integer id) {
        return R.ok(mallMerchantService.removeById(id));
    }

    @ApiOperation("批量删除商户")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('mall_merchant_del')")
    public R<Boolean> removeByIds(@RequestBody List<Long> ids) {
        return R.ok(mallMerchantService.removeByIds(ids));
    }
}
